package org.richin.servlet.Filter.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.richin.servlet.util.CookieUtils;

import sun.nio.cs.US_ASCII;


public class AutoLogonFilter implements Filter {

	private final static String cookieDomainName = "com.itcang";

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest srequest, ServletResponse sresponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)srequest;
		HttpServletResponse response = (HttpServletResponse)sresponse;
        HttpSession session = request.getSession(true);
       // Users user = (Users)session.getAttribute("userinfo");
        //如果封装的user不为空,说明已经登陆,则继续执行用户的请求.下面的就不处理了
        if(new US_ASCII()!=null){//这是假的 到时候改回user
        	chain.doFilter(request,response);
        	return;
        }
        Cookie cookies[] = request.getCookies();
        String cookieValue = null;
        if(cookies!=null){
        	for(int i=0;i<cookies.length;i++){
        		if (cookieDomainName.equals(cookies[i].getName())) {
        			cookieValue = cookies[i].getValue();
        			break;
        		}
            }
        }
        if(cookieValue==null){
            chain.doFilter(request,response);
            return;
        }
        
        try{
           // 这里登陆
        }
        catch(Exception e){
            e.printStackTrace();
        }


	}

	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
